<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/4
 * Time: 17:27
 */
namespace App\Modules\Jobs\Services;
use App\Modules\User\Models\UserTaskDetail;
use App\Modules\User\Services\UserBillService;
use App\Modules\User\Services\UserTaskDetailService;
use Illuminate\Support\Facades\Log;
use Redis;

class UpdateUserDetailService {
    /**
     * 更新用户打赏总额
     * @param $uid
     * @param $value
     */
    public static function UpdateUserRewards($uid,$value)
    {
        $rewardTimes =  Redis::hget('user_task_info:'.$uid,'rewards_count');
        $rewardTotal = Redis::hget('user_task_info:'.$uid,'rewards_total_fee');//+$value;
        if($rewardTimes && $rewardTotal) {
            $rewardTimes++;
            $rewardTotal+=$value;
        }else{
            $info = UserTaskDetail::getUserRewardsInfo($uid);
            $rewardTimes = $info['rewardTimes'];
            $rewardTotal = $info['rewardTotal'];
        }
        Redis::hset('user_task_info:'.$uid,'rewards_count',$rewardTimes);
        Redis::hset('user_task_info:'.$uid,'rewards_total_fee',$rewardTotal);
    }

    /**
     * 更新用户所有已发布的任务数
     * @param $uid
     * @return mixed
     */
    public static function UserTaskNum($uid)
    {
        $task_num = Redis::hget('user_task_info:' . $uid, 'task_count');
        Log::info('$task_num1'.$task_num);
        if ($task_num ) {
            //$task_num = UserTaskDetail::getUserTaskNum($uid);
            $task_num++;
            Log::info('$task_num12'.$task_num);
        }else{
            $task_num = UserTaskDetail::getUserTaskNum($uid);
            Log::info('$task_num3'.$task_num);
        }
        Redis::hset('user_task_info:' . $uid, 'task_count', $task_num);
        Log::info('$task_num4'.$task_num);
        return $task_num;
    }

    /**
     * 更新用户感兴趣总数
     */
    public static function UpdateUserAdoptTotal($uid)
    {
        //$interest = 0;
        $interest = Redis::hget('user_task_info:' . $uid, 'interest_count');
        if ($interest) {
            //$interest = UserTaskDetail::getUserPickTotal($uid);
            $interest++;
        }else{
            $interest  = UserTaskDetail::getUserPickTotal($uid);
        }
        Redis::hset('user_task_info:' . $uid, 'interest_count', $interest);
        return $interest;
    }

    /**
     * 更新用户处理总时长
     * @param $uid
     */
    public static function updateDuration($uid,$value){
        $handleDuration = Redis::hget('user_task_info:' . $uid, 'handle_duration');
        if ($handleDuration) {
            $info = unserialize($handleDuration);
            $info['totalTime']++;
            $info['totalCount']+=$value;
            $handleDuration = serialize($info);
        }else{
            $handleDuration = serialize(UserTaskDetail::handleDuration($uid));
        }
        Redis::hset('user_task_info:'.$uid,'handle_duration',$handleDuration);
        return unserialize($handleDuration);
    }

    /**
     * 更新用户平均打赏
     * @param $uid 打赏发起者人
     * @return array
     */
    public static function updateAverageReward($uid)
    {

        $res = UserTaskDetail::getAverageReward($uid);
        $RewardToUserSum = $res['RewardToUserSum'];
        $RewardToUserNum = $res['RewardToUserNum'];

        Redis::hset('user_task_info:' . $uid, 'RewardToUserSum',$RewardToUserSum);
        Redis::hset('user_task_info:' . $uid, 'RewardToUserNum',$RewardToUserNum);

        return [
            'RewardToUserNum'=>$RewardToUserNum,
            'RewardToUserSum'=>$RewardToUserSum
        ];
    }

    public static function updateRewardOutCount($uid,$value){
        $userRewardSum = Redis::hget('user_task_info'.$uid,'userRewardSum');
        if($userRewardSum){
            $userRewardSum +=$value;
        }else{
            $userRewardSum = UserBillService::getRewardOutCount($uid);
        }
        Redis::hset('user_task_info'.$uid,'userRewardSum',$userRewardSum);
        return $userRewardSum;
    }

}